package com.yanqu.road.server.gameplayer.module.activity.celebration;

import com.yanqu.road.entity.activity.celebration.CelebrationScoreConfig;
import com.yanqu.road.entity.enums.eGamePlayerEventType;
import com.yanqu.road.entity.enums.eSystemId;
import com.yanqu.road.pb.celebration.CelebrationProto;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.base.GeneralModule;
import com.yanqu.road.server.manger.activity.celebration.CelebrationMgr;
import com.yanqu.road.server.manger.config.SystemOpenMgr;
import com.yanqu.road.server.protocol.CrossProtocol;
import com.yanqu.road.server.protocol.Protocol;


/**
 * @Description
 * @Author cwq
 * @Data 2021/7/12 15:49
 */
public class CelebrationModule extends GeneralModule {

    @Override
    public boolean loadData() {
        return true;
    }

    @Override
    public boolean afterLoadData() {
        return true;
    }

    @Override
    public boolean saveData() {
        return true;
    }

    @Override
    public void afterLogin() {
        syncConfig();
        // 同步喜庆值
        synFestiveValue();
    }

    public void initSystem() {
        syncConfig();
        synFestiveValue();
    }

    public CelebrationModule(GamePlayer player) {
        super(player);
    }

    public void syncConfig() {
        if (CelebrationMgr.getActivityInfo() == null || !SystemOpenMgr.systemOpen(player, eSystemId.CelebrationActivity.getValue())) {
            return;
        }
        CelebrationProto.CelebrationActivityMsg.Builder config = CelebrationProto.CelebrationActivityMsg.newBuilder();
        config.setActivityId(CelebrationMgr.getActivityInfo().getActivityId());
        for (CelebrationScoreConfig item : CelebrationMgr.getCelebrationActivityConfig().getScoreConfigList()) {
            CelebrationProto.CelebrationScoreConfigEntity.Builder scoreConfigEntity = CelebrationProto.CelebrationScoreConfigEntity.newBuilder();
            scoreConfigEntity.setId(item.getId());
            scoreConfigEntity.setScore(item.getScore());
            scoreConfigEntity.setAppearanceId(item.getAppearanceId());
            config.addScores(scoreConfigEntity);
        }
        player.sendPacket(Protocol.U_CELEBRATION_CONFIG, config);
    }

    /**
     * 向跨服同步成就值
     */
    public void synFestiveValue() {
        if (CelebrationMgr.getActivityInfo() == null || !SystemOpenMgr.systemOpen(player, eSystemId.CelebrationActivity.getValue())) {
            return;
        }
        if (CelebrationMgr.getActivityBuildLv().longValue() > -1) {
            player.notifyListener(eGamePlayerEventType.CelebrationActivityFestiveCrossTotalValue.getValue(), CelebrationMgr.getActivityBuildLv().longValue());
        } else {
            // 向跨服同步
            CelebrationProto.CelebrationSynBuildLvGameToCrossMsg.Builder gameToCrossMsg = CelebrationProto.CelebrationSynBuildLvGameToCrossMsg.newBuilder();
            gameToCrossMsg.setActivityId(CelebrationMgr.getActivityInfo().getActivityId());
            player.sendPacket(CrossProtocol.C2_CELEBRATION_SYN_BUILD_TOTAL_LV, gameToCrossMsg);
        }
    }
}
